####################################################################
## author:    Robert A. Huber
## contact:   robert.huber@ir.gess.ethz.ch
## file name: pc_uk_predict.R
## Context:   Populism and Climate Sceptism, individuals from BES
## started:   2019-07-09
## Summary:   runs R-Scripts
######################################################################

## CC ####

fit.effR <- effect("populism", mcc.1, xlevels = list(populism=seq(minPop,maxPop, length.out = 2),
                                                     lr = seq(0, 10, length.out = 2)),
                   x.var="populism", confidence.level = 0.95)

PredPropR <- data.frame(fit.effR$model.matrix, fit.effR$prob, fit.effR$lower.prob,
                        fit.effR$upper.prob)

datpo <- PredPropR[c("populism",
            "prob.Climate.changing.due.to.human.activity",
            "prob.Climate.changing.but.not.due.to.human.activity",
            "prob.Climate.not.changing")]

fit.effR <- effect("lr", mcc.1, xlevels = list(lr = seq(0, 10, length.out = 2)),
                   x.var="lr", confidence.level = 0.95)

PredPropR <- data.frame(fit.effR$model.matrix, fit.effR$prob, fit.effR$lower.prob,
                        fit.effR$upper.prob)

datlr <- PredPropR[c("lr",
            "prob.Climate.changing.due.to.human.activity",
            "prob.Climate.changing.but.not.due.to.human.activity",
            "prob.Climate.not.changing")]

datcc <- data.frame(iv = rep(c("populism", "left-right"), each = 2),
                    value = rep(c("Min", "Max"), 2),
                    due.to.human.activity = c(datpo$prob.Climate.changing.due.to.human.activity, datlr$prob.Climate.changing.due.to.human.activity),
                    not.due.to.human.activity = c(datpo$prob.Climate.changing.but.not.due.to.human.activity, datlr$prob.Climate.changing.but.not.due.to.human.activity),
                    not.changing = c(datpo$prob.Climate.not.changing, datlr$prob.Climate.not.changing))

datcc <- datcc %>%
  gather(due.to.human.activity:not.changing, value = "pred", key = "dv")

## EP ####

fit.effR <- effect("populism", meP.1, xlevels = list(populism=seq(minPop,maxPop, length.out = 2)),
                   x.var="populism", confidence.level = 0.95)

PredPropR <- data.frame(fit.effR$model.matrix, fit.effR$prob, fit.effR$lower.prob,
                        fit.effR$upper.prob)

datpo <- PredPropR[c("populism",
            "prob.Gone.too.far",
            "prob.About.right",
            "prob.Not.enough")]

fit.effR <- effect("lr", meP.1, xlevels = list(lr = seq(0, 10, length.out = 2)),
                   x.var="lr", confidence.level = 0.95)

PredPropR <- data.frame(fit.effR$model.matrix, fit.effR$prob, fit.effR$lower.prob,
                        fit.effR$upper.prob)

datlr <- PredPropR[c("lr",
            "prob.Gone.too.far",
            "prob.About.right",
            "prob.Not.enough")]

datep <- data.frame(iv = rep(c("populism", "left-right"), each = 2),
                    value = rep(c("Min", "Max"), 2),
                    too.far = c(datpo$prob.Gone.too.far, datlr$prob.Gone.too.far),
                    About.right = c(datpo$prob.About.right, datlr$prob.About.right),
                    Not.enough = c(datpo$prob.Not.enough, datlr$prob.Not.enough))

datep <- datep %>%
  gather(too.far:Not.enough, value = "pred", key = "dv")

## EG ####

fit.effR <- effect("populism", meG.1, xlevels = list(populism=seq(minPop,maxPop, length.out = 2)),
                   x.var="populism", confidence.level = 0.95)


datpo <- data.frame(fit.effR)

fit.effR <- effect("lr", meG.1, xlevels = list(lr = seq(0, 10, length.out = 2)),
                   x.var="lr", confidence.level = 0.95)

datlr <- data.frame(fit.effR)

dateg <- data.frame(iv = rep(c("populism", "left-right"), each = 2),
                    value = rep(c("Min", "Max"), 2),
                    dv = "eg",
                    pred = c(datpo$fit, datlr$fit))

datpred <- rbind(datcc, datep, dateg)


datpred$grp <- c(rep("The worlds climate...", 12), rep("Environmental Protection...", 12), rep("Environment vs. Growth", 4))

datpred$dv <- factor(datpred$dv,
                     levels = c("due.to.human.activity", "not.due.to.human.activity", "not.changing",
                                "too.far", "About.right", "Not.enough",
                                "eg"),
                     labels = c("...is changing due\nto human activity", "...is changing but\nnot due to human activity", "...is not changing",
                                            "... has gone too far", "... is about right", "... has not gone far enough",
                                            "Environment vs Growth"))

datpred$IVDV <- paste0(datpred$dv, "-", datpred$iv)

datpred$iv <- factor(datpred$iv, levels = c("left-right", "populism"),
                     labels = c("Pol. Ideology", "Populism"))
                 
p_pred <- ggplot(subset(datpred, dv!= "Environment vs Growth"), aes(x=dv, y=pred, group = iv)) +
  geom_line(aes(group = IVDV), position = position_dodge(width = .5), colour = "lightgrey", lwd = 1.1) +
  geom_point(aes(colour = iv, shape = value), position = position_dodge(width = 0.5), size = 1.8) +
  coord_flip() +
  facet_grid(grp~., scales = "free") +
  ggthemes::theme_tufte() +
  labs(x="Dependent Variable", y = "Predicted Probability") +
  scale_colour_discrete("Independent Variable") +
  scale_shape_discrete("Value") +
  theme(text = element_text(size=20))

ggsave(filename = "pred.pdf")
ggsave(filename = "pred.png")

p_pred <- ggplot(subset(datpred, dv== "Environment vs Growth"), aes(x=dv, y=pred, group = iv)) +
  geom_line(aes(group = IVDV), position = position_dodge(width = .5), colour = "lightgrey", lwd = 1.1) +
  geom_point(aes(colour = iv, shape = value), position = position_dodge(width = 0.5), size = 1.8) +
  coord_flip() +
  facet_grid(grp~., scales = "free") +
  ggthemes::theme_tufte() +
  labs(x="Dependent Variable", y = "Predicted Probability") +
  scale_colour_discrete("Independent Variable") +
  scale_shape_discrete("Value") +
  theme(text = element_text(size=20))

ggsave(filename = "pred_EG.pdf")
ggsave(filename = "pred_EG.png")
